package sucre

class AldeaController {

    def loginService
    def beforeInterceptor = [action:this.&auth]
    def auth = {
        def result =loginService.validarAcceso(
            session["seguridad_usuario"],
            session["seguridad_funcionesHabilitadas"],
            controllerName,actionName,request.getRemoteAddr().toString())
        if(!result) {
            redirect(controller:"login",action:"auth",params:['acceso':false])
            return false
        }
    }
    def index = {}
    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def parametersService
    def create = {
        def aldeaInstance = new Aldea()
        params.eOc='edit'
        aldeaInstance.properties = params
        aldeaInstance=Aldea.findAll().getAt(0)
        return [aldeaInstance:aldeaInstance,eOc:params.eOc]
    }

    def save = {
        def listaErrores=[]
        def aldeaInstance = new Aldea(params)
        if(params.eOc=='edit'){
            println  "aldeaInstance: " + aldeaInstance
            aldeaInstance=Aldea.get(params.id)
            aldeaInstance.properties=params
        }
        Aldea.withTransaction{ status ->
            try{
                if(!aldeaInstance.hasErrors() && aldeaInstance.save(flush:true)) {
                    //def.titular.actualizado
                    flash.message=message(code:'def.registro.actualizado', args:[aldeaInstance])
                }
            }catch(Exception e){
                println e
                listaErrores.add(message(code:'def.aldea.error.Creando'))
                status.setRollbackOnly()
            }
        }
        render(view:'create',model:[aldeaInstance:aldeaInstance,
                eOc:params.eOc,upd:'si',listaErrores:listaErrores])
    }

}
